<?php
namespace app\controller;

use app\validate\User;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

class Verify
{
    public function index(){
        try{
            //整体返回一个对象,batch是可也返回多个错误。
            validate(User::class)->batch('true')->scene('edit')->check([
                'name' => '蜡笔小新',
                'price' => 90,
                'email' => 'xiaoxin@163.com'
            ]);
        }catch(ValidateException $e){
            dump($e->getError());
        }
    }

    public function rule(){
//        $validate = Validate::rule([
//            'name' => 'require|max:20',         //不得为空，不得大于 20 位
//            'price' => 'number|between:1,100',  //必须是数值，1-100 之间
//            'email' => 'email'                  //邮箱格式要正确
//        ]);

        $validate = Validate::rule([
            'name' => ValidateRule::isRequire()->max(20),         //不得为空，不得大于 20 位
            'price' => ValidateRule::isNumber()->between([1,100]),  //必须是数值，1-100 之间
            'email' => ValidateRule::isEmail(),                  //邮箱格式要正确
            //'test'  =>  'length:4',

            //和表的字段做比较
            'username'  =>  'unique:user'
        ]);

        $result = $validate->batch('true')->check([
            'name' => '啊',
            'price' => 20,
            'email' => 'xiaoxin@163.com',
            'test'  => '汉字',
            'username' => '蜡笔小新'
        ]);

        if(!$result){
            dump($validate->getError());
        }
    }

    public function single(){
        dump(Validate::isEmail('xiaoxin163.com'));
        dump(Validate::isRequire('xiaoxin163.com'));
    }
}